# -*- coding: utf-8 -*-
from PyQt4 import QtCore, QtGui
from ui_searchResults import Ui_SearchResultsFrame

class SearchResults(QtGui.QFrame):
	def __init__(self, controller, mode=""):
		QtGui.QFrame.__init__(self)

		self.controller = controller

        # Set up the user interface from Designer.
		self.ui = Ui_SearchResultsFrame()
		self.ui.setupUi(self)

		self.objects = []
		self.names = []

		self.setMode(mode)

		self.connect(self.ui.hidePushButton, QtCore.SIGNAL("clicked()"), self.close)
		self.connect(self.ui.searchResultListWidget, QtCore.SIGNAL("itemClicked(QListWidgetItem *)"), self.updateItemShown)
		self.connect(self.ui.addPushButton, QtCore.SIGNAL("clicked()"), self.addItem)


	def addCompounds(self, compounds):
		for i in compounds:
			if i.name not in self.names:
				self.objects.append(i)
				self.names.append(i.name)

		for i in self.names[self.ui.searchResultListWidget.count():]:
			self.ui.searchResultListWidget.insertItem(0, i)

	def addReferences(self, references):
		for i in references:
			if i.name not in self.names:
				self.objects.append(i)
				self.names.append(i.name)

		for i in self.names[self.ui.searchResultListWidget.count():]:
			self.ui.searchResultListWidget.insertItem(0, i)

	def addCatalysts(self, catalysts):
		for i in catalysts:
			if i.name not in self.names:
				self.objects.append(i)
				self.names.append(i.name)

		for i in self.names[self.ui.searchResultListWidget.count():]:
			self.ui.searchResultListWidget.insertItem(0, i)

	def setMode(self, mode):
		self.mode = mode
		if self.mode == "Compound":
			self.ui.searchInfoLabel.setText("Compound Search")
			self.ui.addReactantRadioButton.hide()
			self.ui.addProductRadioButton.hide()
			self.ui.addCatalystRadioButton.hide()
			self.ui.addPushButton.hide()
		elif self.mode == "Reference":
			self.ui.searchInfoLabel.setText("Reference Search")
			self.ui.addReactantRadioButton.hide()
			self.ui.addProductRadioButton.hide()
			self.ui.addCatalystRadioButton.hide()
			self.ui.addPushButton.hide()
		elif self.mode == "Adding Reactant":
			self.ui.searchInfoLabel.setText("Adding")
			self.ui.addReactantRadioButton.show()
			self.ui.addProductRadioButton.show()
			self.ui.addCatalystRadioButton.hide()
			self.ui.addPushButton.show()
			self.ui.addReactantRadioButton.setChecked(True)
		elif self.mode == "Adding Product":
			self.ui.searchInfoLabel.setText("Adding")
			self.ui.addReactantRadioButton.show()
			self.ui.addProductRadioButton.show()
			self.ui.addCatalystRadioButton.hide()
			self.ui.addPushButton.show()
			self.ui.addProductRadioButton.setChecked(True)
		elif self.mode == "Adding Catalyst":
			self.ui.searchInfoLabel.setText("Adding")
			self.ui.addReactantRadioButton.hide()
			self.ui.addProductRadioButton.hide()
			self.ui.addCatalystRadioButton.show()
			self.ui.addPushButton.show()
			self.ui.addCatalystRadioButton.setChecked(True)
		elif self.mode == "Adding Reference":
			self.ui.searchInfoLabel.setText("Adding")
			self.ui.addReactantRadioButton.hide()
			self.ui.addProductRadioButton.hide()
			self.ui.addCatalystRadioButton.hide()
			self.ui.addPushButton.show()
		else:
			if mode:
				self.ui.searchInfoLabel.setText(mode)
			else:
				self.ui.searchInfoLabel.setText("(blank search)")
			self.ui.addReactantRadioButton.hide()
			self.ui.addProductRadioButton.hide()
			self.ui.addCatalystRadioButton.hide()
			self.ui.addPushButton.hide()

	def clear(self):
		print "trying to clear"
		self.mode = ""
		self.ui.searchResultListWidget.clear()
		self.objects = []
		self.names = []
		self.ui.searchInfoLabel.setText("(blank search)")

	def updateItemShown(self, item):
		if not self.mode or self.mode.find("Adding") != -1:
			return

		obj = self.objects[self.ui.searchResultListWidget.currentRow()]

		if self.mode == "Compound" or self.mode == "Reactants" or self.mode == "Products":
			self.controller.setViewCurrentCompound(obj)
		elif self.mode == "Reference":
			self.controller.setViewCurrentReference(obj)

	def addItem(self):
		if self.mode.find("Adding") != -1:
			if self.ui.searchResultListWidget.currentRow() != -1:
				if self.mode == "Adding Reference":
					self.controller.addToReaction(self.objects[self.ui.searchResultListWidget.currentRow()], "Reference")
				elif self.ui.addProductRadioButton.isChecked():
					self.controller.addToReaction(self.objects[self.ui.searchResultListWidget.currentRow()], "Product")
				elif self.ui.addReactantRadioButton.isChecked():
					self.controller.addToReaction(self.objects[self.ui.searchResultListWidget.currentRow()], "Reactant")
				elif self.ui.addCatalystRadioButton.isChecked():
					self.controller.addToReaction(self.objects[self.ui.searchResultListWidget.currentRow()], "Catalyst")